package com.xy_pro.xymanager.helper;

import org.apache.poi.ss.usermodel.*;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.web.multipart.MultipartFile;

import java.io.IOException;
import java.io.InputStream;
import java.util.*;

public class ExcelHelper {
    public static String TYPE = "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet";

    public static boolean hasExcelFormat(MultipartFile file) {
        return TYPE.equals(file.getContentType());
    }

    public static List<Map<String, Object>> excelToMaps(InputStream is) {
        try (Workbook workbook = new XSSFWorkbook(is)) {
            Sheet sheet = workbook.getSheetAt(0);
            Iterator<Row> rows = sheet.iterator();

            List<Map<String, Object>> dataList = new ArrayList<>();

            if (!rows.hasNext()) {
                return dataList;
            }

            // 获取表头
            Row headerRow = rows.next();
            List<String> headers = new ArrayList<>();
            Iterator<Cell> headerCells = headerRow.iterator();
            while (headerCells.hasNext()) {
                headers.add(headerCells.next().getStringCellValue());
            }

            // 确保包含学生姓名字段
            if (!headers.contains("学生姓名")) {
                throw new IllegalArgumentException("Excel文件必须包含'学生姓名'列");
            }

            // 处理数据行
            while (rows.hasNext()) {
                Row currentRow = rows.next();
                Map<String, Object> rowData = new LinkedHashMap<>();

                for (int i = 0; i < headers.size(); i++) {
                    if (currentRow.getCell(i) == null) {
                        rowData.put(headers.get(i), null);
                        continue;
                    }

                    Cell cell = currentRow.getCell(i);
                    switch (cell.getCellType()) {
                        case STRING:
                            rowData.put(headers.get(i), cell.getStringCellValue());
                            break;
                        case NUMERIC:
                            if (DateUtil.isCellDateFormatted(cell)) {
                                rowData.put(headers.get(i), cell.getDateCellValue());
                            } else {
                                rowData.put(headers.get(i), cell.getNumericCellValue());
                            }
                            break;
                        case BOOLEAN:
                            rowData.put(headers.get(i), cell.getBooleanCellValue());
                            break;
                        case FORMULA:
                            rowData.put(headers.get(i), cell.getCellFormula());
                            break;
                        default:
                            rowData.put(headers.get(i), null);
                    }
                }

                dataList.add(rowData);
            }

            return dataList;
        } catch (IOException e) {
            throw new RuntimeException("Failed to parse Excel file: " + e.getMessage());
        }
    }
}
